﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.DAL;
using MINERP.DAL.Statistics;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmProductPrice : Form
    {
        private BLL.Statistics.StatisticsBLL SSBLL = new MINERP.BLL.Statistics.StatisticsBLL();

        public FrmProductPrice()
        {
            InitializeComponent();
        }
        #region 单件
        //单件
        private static FrmProductPrice _instance;
        public static FrmProductPrice Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmProductPrice();
                }
                else if (_instance.IsDisposed)
                {
                    _instance = new FrmProductPrice();
                }
                return _instance;
            }
        }
        #endregion

        #region Load
        private void FrmProductPrice_Load(object sender, EventArgs e)
        {
            //加载产品分类

            List<Tb_PorductCategory> listCategory = ProductStatDAL.GetAllCategory();

            comboBoxEdit1.Properties.Items.Clear();
            comboBoxEdit1.Properties.Items.Add("全部");
            if (listCategory != null)
            {
                foreach (var category in listCategory)
                {
                    comboBoxEdit1.Properties.Items.Add(category.ProductCategoryName);
                }
                comboBoxEdit1.Text = "全部";
            }
            DateTime now = DateTime.Now;
            string nowStr = now.ToString("yyyy-MM-dd");
            dtStartTime.Text = nowStr;
            dtEndTime.Text = nowStr;




        }
        #endregion


        private void btnOK_Click(object sender, EventArgs e)
        {
            btnOK.Enabled = false;
            DateTime startTime = dtStartTime.DateTime;
            DateTime endTime = dtEndTime.DateTime;
            if (endTime < startTime)
            {
                MessageBox.Show("请选择正常的时间范围!");
                return;
            }
            if (!string.IsNullOrEmpty(comboBoxEdit1.Text) && !string.IsNullOrEmpty(comboBoxEdit2.Text))
            {
                DataSet dsResult = ProductStatDAL.GetFactoryProductS(comboBoxEdit1.Text, comboBoxEdit2.Text, startTime, endTime);
                dsResult.WriteXml(@"C:\Windows\xmldata\车间产品统计.xml", XmlWriteMode.WriteSchema);
                report.Preview = previewControl1;
                report.RegisterData(dsResult);
                report.Load(Application.StartupPath + "\\车间产品统计.frx");
                report.SetParameterValue("startTime", startTime);
                report.SetParameterValue("endTime", endTime);
                previewControl1.Refresh();
                report.Show();
            }
            btnOK.Enabled = true;


            /*
            DataTable productPrictDT = SSBLL.GetProductPrice(startTime, endTime);
            productPrictDT.TableName = "tableProductPrice";
            DataTable startAndEndDT = SSBLL.GetTimeDT(startTime, endTime);
            startAndEndDT.TableName = "tableStartAndEndTime";
            DataSet set = new DataSet();
            set.Tables.Add(productPrictDT);
            set.Tables.Add(startAndEndDT);
            set.WriteXml(@"C:\Windows\xmldata\ProductPrice.xml", XmlWriteMode.WriteSchema);

            //显示报表
            //previewControl1
            report.Preview = previewControl1;
            report.RegisterData(set);
            report.Load(Application.StartupPath + "\\frxProductPrice.frx");
            previewControl1.Refresh();
            previewControl1.Show();
            report.Show();
             */
        }

        private void FrmProductPrice_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance.Dispose();
            _instance = null;
        }

        private void comboBoxEdit1_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            //根据分类绑定产品列表
            if (!string.IsNullOrEmpty(comboBoxEdit1.Text))
            {
                List<string> ListProductName = ProductStatDAL.GetProductByCategroyName(comboBoxEdit1.Text);
                comboBoxEdit2.Properties.Items.Clear();
                comboBoxEdit2.Properties.Items.Add("全部");
                if (ListProductName != null)
                {
                    foreach (var productName in ListProductName)
                    {
                        comboBoxEdit2.Properties.Items.Add(productName);
                    }

                    comboBoxEdit2.Text = "全部";
                }
            }
        }
    }
}
